const { getMatchById } = require('./dota2api/index')
const { getPartSize, getGameMode, getLobbyType, isWin, getHeroCnName } = require('./utils/dota2helper')

const messageList = [
    {
        content: '单排上的分 组排还回去',
    }
]

async function getReport(matchId, accountId) {
    return new Promise(async (resolve, reject) => {
        try {
            const matchInfo = await getMatchById(matchId)
            const playerInfo = matchInfo.players.find(player => { return player.account_id === accountId })
            const timeNow = new Date().getTime()
            let recentMatchTime = (matchInfo.start_time + matchInfo.duration) * 1000
            let time = timeNow - recentMatchTime
            resolve(`
【比赛时间】 ${getStartTime(matchInfo.start_time)}(${formatTime(time)})
【模式】 ${getPartSize(matchInfo.party_size)}-${getGameMode(matchInfo.game_mode)}-${getLobbyType(matchInfo.lobby_type)}
【持续时间】 ${formatDuration(matchInfo.duration)}
【比分】 夜魇${matchInfo.dire_score} VS ${matchInfo.radiant_score}天辉
【玩家】 ${playerInfo.personaname}[${playerInfo.account_id}]
【胜负】 ${isWin(playerInfo.player_slot, playerInfo.radiant_win) ? '胜利' : '失败'}
【KAD】 ${playerInfo.kills}杀/${playerInfo.assists}助/${playerInfo.deaths}死
【英雄】 ${getHeroCnName(playerInfo.hero_id)}
【英雄伤害】 ${playerInfo.hero_damage}/${getHeroDemagePer(matchInfo.players, accountId)}
【英雄治疗】 ${playerInfo.hero_healing}
【建筑伤害】 ${playerInfo.tower_damage}
【XPM/GPM】 ${playerInfo.xp_per_min}/${playerInfo.gold_per_min}`)
        } catch (error) {
            reject(error)
        }
    })
}

function formatDuration(time) {
    return `${(time / 60).toFixed(0)}:${time % 60}`
}

function getTeamfightPer(teamFightParticipation) {
    if (!teamFightParticipation) return '未知'
    return `${(teamFightParticipation * 100).toFixed(2)}%`
}

function getHeroDemagePer(players, accountId) {
    const playerInfo = players.find(player => { return player.account_id === accountId })
    const teamPlayers = players.filter(player => { return player.isRadiant === playerInfo.isRadiant })
    let teamDemageTotal = 0
    teamPlayers.forEach(player => {
        teamDemageTotal += player.hero_damage
    });
    return `${(playerInfo.hero_damage / teamDemageTotal * 100).toFixed(2)}%`
}

function getChat(chatList, playerSlot) {
    if (!chatList) return '未知'
    const list = chatList.filter(chatItem => {
        return chatItem.type === 'chat' && chatItem.player_slot === playerSlot
    })
    if (list.length > 0) {
        const messageList = []
        list.forEach(item => {
            messageList.pish(item.key)
        })
        return messageList.join('、')
    }
    return '无'
}

function formatTime(time) {
    const minutes = Math.floor(time / (60 * 1000))
    return `${minutes}分钟前`
}

function getStartTime(time) {
    const startTime = new Date(time * 1000)
    const year = startTime.getFullYear()
    const month =  formatNumber((startTime.getMonth() + 1))
    const day =  formatNumber(startTime.getDate())
    const hour =  formatNumber(startTime.getHours())
    const minute = formatNumber(startTime.getMinutes())
    const second = formatNumber(startTime.getSeconds())
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

function formatNumber(number){
    return number < 10 ? '0' + number : number
}

module.exports = {
    getReport
}